<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <style>
        body {
            background-color: #1c3f69;
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .box {
            background-color: white;
            padding: 40px;
            border-radius: 8px;
            width: 320px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
        }
        input[type=text], input[type=password] {
            width: 100%;
            padding: 12px;
            margin: 8px 0;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        .button {
            width: 100%;
            padding: 12px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            margin-top: 10px;
        }
        .link-button {
            display: block;
            text-align: center;
            margin-top: 10px;
            color: #007bff;
            text-decoration: none;
        }
        .toast {
            position: fixed;
            top: 20px;
            right: 20px;
            background-color: rgba(0,0,0,0.7);
            color: white;
            padding: 10px 20px;
            border-radius: 4px;
            display: none;
        }
    </style>
</head>
<body>

<div class="box">
    <h2 style="text-align:center;">登录</h2>
    <input type="text" id="username" placeholder="用户名">
    <input type="password" id="password" placeholder="密码">
    <button class="button" onclick="login()">登录</button>
    <a class="link-button" href="register.html">去注册</a>
</div>

<div id="toast" class="toast"></div>

<script>
    function showToast(message) {
        const toast = document.getElementById('toast');
        toast.innerText = message;
        toast.style.display = 'block';
        setTimeout(() => {
            toast.style.display = 'none';
        }, 3000);
    }

    function login() {
        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;

        fetch('/api/user/login', {
            method: 'POST',
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            body: `username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`
        })
            .then(response => response.json())
            .then(result => {
                showToast(result.message);

                if (result.code === 200) {
                    setTimeout(() => {
                        window.location.href = 'index.html';
                    }, 1000);
                }
            })
            .catch(() => {
                showToast('请求失败，请稍后重试');
            });
    }
</script>


</body>
</html>
